import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_svg/svg.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:sleep/db/export.dart';
import 'package:sleep/db/merroy_db.dart';
import 'package:sleep/help_style.dart';
import 'package:sleep/page/blue/blue_scan_screen.dart';
import 'package:sleep/page/fatigue/comp/title_chart.dart';
import 'package:sleep/page/sleep/comp/sleep_chat.dart';
import 'package:sleep/page/sleep/comp/sleep_state.dart';
import 'package:sleep/page/sleep/detail_page.dart';
import 'package:sleep/comp/home_bottom.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sleep/page/user.dart';
import 'package:sleep/provider/chat_config_provider.dart';
import 'package:sleep/provider/db_provider.dart';
import 'package:sleep/provider/wave_deatil_provider/wave_provider.dart';

const name = 'User';

class Sleep extends StatelessWidget {
  const Sleep({Key? key}) : super(key: key);

  jumpDetail(BuildContext context) {
    Navigator.of(context)
        .push(CupertinoPageRoute(builder: (context) => DetailPage()));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "Sleep",
          style: HelpStyle.titleStyle,
        ),
        actions: [
          Consumer(
            builder: (context, ref, child) {
              return IconButton(
                  padding: EdgeInsets.zero,
                  onPressed: () {
                    ref.read(sleepProvider).analyze.clear();
                    ref.read(pulseProvider).analyze.clear();
                    MerroyDB.share().sleepClear();
                  },
                  icon: Icon(
                    Icons.refresh,
                    color: HelpStyle.primarycolor,
                  ));
            },
          ),
          Consumer(
            builder: (context, ref, child) {
              final sleep = ref.watch(sleepProvider);
              final needSave = sleep.needSave;
              return IconButton(
                  padding: EdgeInsets.zero,
                  onPressed: () {
                    if (!needSave) EasyLoading.showToast("Recording....");

                    if (needSave) EasyLoading.showToast("save in /Download");
                    sleep.changeSave(!needSave, sleep: true);
                  },
                  icon: Icon(
                    needSave ? Icons.draw : Icons.mode_edit,
                    color: needSave
                        ? HelpStyle.primarycolor
                        : HelpStyle.grayTitleColor,
                  ));
            },
          ),
          IconButton(
              padding: EdgeInsets.zero,
              onPressed: () {
                Navigator.of(context).push(
                  MaterialPageRoute(builder: (context) => BlueScanScreen()),
                );
              },
              icon: Icon(
                Icons.bluetooth,
                color: HelpStyle.primarycolor,
              )),
        ],
        backgroundColor: Colors.transparent,
        elevation: 0.0,
        leading: Padding(
          padding: const EdgeInsets.all(8.0),
          child: ClipRRect(
            borderRadius: BorderRadius.circular(24),
            child: ColoredBox(
                color: HelpStyle.backgroundColor,
                child: Consumer(
                  builder: (context, ref, child) {
                    final db = ref.read(dbProvider);

                    return IconButton(
                        padding: EdgeInsets.zero,
                        onPressed: () {
                          Navigator.of(context).push(CupertinoPageRoute(
                              builder: (c) => UserPage(
                                    type: UserType.sleep,
                                    person: db.getPerson(kSleepPerson),
                                  )));
                        },
                        icon: Icon(
                          Icons.person,
                          color: HelpStyle.primarycolor,
                        ));
                  },
                )),
          ),
        ),
      ),
      backgroundColor: HelpStyle.backgroundColor,
      body: Padding(
        padding: EdgeInsets.only(
          top: 0,
          left: HelpStyle.margin,
          right: HelpStyle.margin,
        ),
        child: CustomScrollView(
          slivers: [
            SliverToBoxAdapter(
              child: SizedBox(height: HelpStyle.cellMargin),
            ),

            SliverToBoxAdapter(
              child: SleepStateWidget(),
            ),
            SliverToBoxAdapter(
              child: SizedBox(height: HelpStyle.cellMargin),
            ),

            SliverToBoxAdapter(
              child: Consumer(
                builder: (BuildContext context, WidgetRef ref, Widget? child) {
                  WaveDataNotifier sleep = ref.watch(sleepProvider);
                  final chatConfig = ref.watch(chatConfigProvider).sleepChat;
                  return TitleChart(
                    title: "Breathe",
                    icon: SvgPicture.asset('assets/ent.svg',
                        width: 24, height: 24, color: Color(0xff34548F)),
                    spots: sleep.spots,
                    chatConfig: chatConfig,
                  );
                },
              ),
            ),
            SliverToBoxAdapter(
              child: SizedBox(height: HelpStyle.cellMargin),
            ),

            // SleepStateWidget
            SliverToBoxAdapter(
              child: SleepWidget(
                detialOnTap: () => jumpDetail(context),
              ),
            ),
            SliverToBoxAdapter(
              child: SizedBox(height: HelpStyle.cellMargin),
            ),
            SliverToBoxAdapter(
              child: Consumer(builder:
                  (BuildContext context, WidgetRef ref, Widget? child) {
                WaveDataNotifier pulse = ref.watch(pulseProvider);
                final chatConfig = ref.watch(chatConfigProvider).pulseChat;
                return TitleChart(
                  icon: Icon(
                    Icons.favorite,
                    color: Color(0xff652639),
                  ),
                  title: "Pulse",
                  spots: pulse.spots,
                  chatConfig: chatConfig,
                );
              }),
            ),
            SliverToBoxAdapter(
              child: SizedBox(height: HelpStyle.cellMargin),
            ),
            const SliverToBoxAdapter(child: HomeBottom()),
            SliverToBoxAdapter(
              child: SizedBox(height: HelpStyle.cellMargin),
            ),
          ],
        ),
      ),
    );
  }
}
